//index.js
//获取应用实例
var base = getApp();
var curpage=1;//当前页数
var size=10;//每页显示条数
Page({
  data: {
    goodsList: [],
    url: base.path.url,
    isReach:1,//是否允许下拉 0为不允许
    coordNo:1,//当前产品
  },
  // //下拉刷新
  // onPullDownRefresh: function () {
  //   this.onLoad();
  //   wx.stopPullDownRefresh();
  // },
  scrollTopFun: function (e) {
    // 容器滚动时将此时的滚动距离赋值给 this.data.scrollTop
    var coordNo = 1;
    if (e.detail.scrollTop>250){
      coordNo = Math.floor((e.detail.scrollTop+550) / 400)
    }
    this.setData({
      coordNo: coordNo
    });
  },
//去详情页
  goDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../goods_detail/goods_detail?id=' + id
    })
  },

  onShow: function () {
    var that = this;
    base.sessionId(base);
    curpage=1;
    that.data.goodsList=[];
    that.getList();
  },
  onReachBottom: function () {
    //下拉
    var that = this;
    var isReach=that.data.isReach;
    if (isReach==0){
      return;
    }
    that.getList()
  },
 
  onShareAppMessage: function () {
    return {
      title: base.user.shareTitle,
      desc: base.user.shareDesc,
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }, 


  getList: function () {
    var that = this;
    var list = that.data.goodsList;
    wx.request({
      url: base.path.api + 'Goods.GetGoodsList',
      data: {
        size: size,
        aid: base.user.aid,
        curror: curpage
      },
      method: "POST",
      header: base.user.header,
      success: function (response) {
        var result = response.data;
        var goodsList = [];
        if (result.ret == 200) {
          for (var i = 0; i < result.data.length; i++) {

            goodsList.push(result.data[i]);
          }
          list = list.concat(goodsList);
          that.setData({
            goodsList: list
          });
          if (result.data.length >= size) {
            curpage++;
            that.data.isReach=1;
          }else{
            that.data.isReach = 0;
          }

        }
      },
    })
  }
})
